package com.team20.map_demo.controller;

import com.team20.map_demo.domain.Administrator;
import com.team20.map_demo.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdministratorController {
    @Autowired
    private AdministratorService AdministratorService;

    /*@RequestMapping("/login")
    public String login(String adminName, String adminPassword, Model model) {
        String msg = null;
        if(AdministratorService.getAdminByNameAndPwd(adminName, adminPassword) == null) {
            msg = "用户名或者密码错误";
            model.addAttribute("msg", msg);
            return "admin_login";
        }
        else {
            msg = "登录成功";
            model.addAttribute("msg", msg);
            return "admin_access";
        }
    }*/

    /*@RequestMapping("/login")
    public String login(Model model, String adminName, String adminPassword, HttpSession session){
        Administrator administrator = AdministratorService.getAdminByName(adminName);
        String msg = null;
        if(administrator == null){
            msg = "用户名或密码错误";
            model.addAttribute("msg",msg);
            return "admin_login";
        }else {
            String pwd = administrator.getPassword();
            if (adminPassword != null && pwd.equals(adminPassword)){
                msg = "登录成功";
                model.addAttribute("msg",msg);
                session.setAttribute("one_login", administrator);
                return "admin_access";
            }else{
                msg = "用户名或密码错误";
                model.addAttribute("msg",msg);
                return "admin_login";
            }
        }
    }*/

    @RequestMapping("/login")
    public String login(){
        return "admin_login";
    }

    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    @ResponseBody
    public Map apiLogin(String adminName, String adminPassword, HttpSession session){
        Administrator administrator = AdministratorService.getAdminByName(adminName);
        String msg = null;
        Map<String, Object> rs = new HashMap<>();

        if(administrator == null){
            msg = "用户名或密码错误";
            rs.put("msg", msg);
            rs.put("ok", false);
        } else {
            String pwd = administrator.getPassword();
            if (adminPassword != null && pwd.equals(adminPassword)){
                msg = "登录成功";
                rs.put("msg", msg);
                rs.put("ok", true);
                session.setAttribute("one_login",administrator);
            } else {
                msg = "用户名或密码错误";
                rs.put("msg", msg);
                rs.put("ok", false);
            }
        }
        return rs;
    }
}
